const { SELECTMOTE, SELECTNONE, SELECTLINE, SELECTREMOVELINE, CHOOSEATTACK, STATUS_ERR, STATUS_NORMAL } = require("./name");
/**
 * 外部传参数据
 */
let canvas = null;
let canvasGrid = null;
let ctx = null
let select = null
let choose = null;
let canvasModule = null
let workspace = null
/**
 * 动画数据
 */
let animeCounter = 0;
/**
 * 常量定义
 */
const MOTE_WIDTH = 50
const STATUS_WIDTH = 10
const ANIME_BOARDER = 10
const GRID_WIDTH = 25
const CANVAS_WIDTH = 650
const CANVAS_HEIGHT = 500

module.exports.setDataIntoCanvasModule = function (newWorkspace, newCanvas, newCanvasGrid, newCtx, newSelect, newChoose, newCanvasModule) {
    canvas = newCanvas;
    canvasGrid = newCanvasGrid;
    ctx = newCtx;
    gridCtx = canvasGrid.getContext("2d");
    select = newSelect;
    choose = newChoose;
    canvasModule = newCanvasModule;
    workspace = newWorkspace;
    //绘制栅格背景
    drawGridBackground(gridCtx);
}
module.exports.canvasClickEventHandler = function (e) {
    let type = select.type
    if (type == SELECTMOTE) {
        //如果当前持有物件点击 则是一个部署行为
        if (!workspace.isRunning) {
            //如果处于未运行期间才可以添加设备
            workspace.canvasCallbackAddMote(e)
        } else {
            //如果已经运行 则取消添加设备动作
            workspace.cancelDevice()
        }
    } else if (type == SELECTNONE || type == SELECTLINE || type == SELECTREMOVELINE || type == CHOOSEATTACK) {
        //获取到当前被点击的物件
        clickObject = getCLickObject(e);
        if (clickObject != null) {
            if (type == SELECTLINE) {
                if (choose.a == null) {
                    //如果当前第一个元素还没有被选中
                    choose.a = clickObject;
                } else if (choose.b == null) {
                    choose.b = clickObject;
                    workspace.canvasCallbackAddLine();
                }
            }
            else if (type == SELECTNONE) {
                //获得需要展示的点
                workspace.choose.a = clickObject;
                workspace.display = true;
            }
            else if (type == CHOOSEATTACK) {
                choose.b = clickObject;
                workspace.canvasCallbackAddAttack();
            }
        }
    }
}
module.exports.canvasHoverEventHandler = function (e) {
    clearFrame();
    refreshMotes();
    refreshLines();
    //当前选择了物件 需要刷新屏幕
    if (select.type == SELECTMOTE) {
        drawMote(select.entity, e.layerX, e.layerY);
    } else if (select.type == SELECTLINE) {
        if (choose.a != null) {
            drawLine(choose.a.x, choose.a.y, e.layerX, e.layerY);
        }
    }
}
module.exports.runloop = function () {
    clearFrame();
    refreshMotes();
    refreshLines();
    animeCounter += 1
    if (animeCounter % ANIME_BOARDER == 0) {
        animeCounter = 0;
    }
    workspace.canvasCallbackLoop()
}
module.exports.RUNLOOPTIME = 50
module.exports.ATTACKTIME = 3500

function clearFrame() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function drawGridBackground(thisCtx) {
    thisCtx.globalAlpha=0.2;
    thisCtx.lineWidth = 0.5;
    for (let i = 0; i <= CANVAS_HEIGHT / GRID_WIDTH; ++i) {
        for (let j = 0; j <= CANVAS_WIDTH / GRID_WIDTH; ++j) {
            thisCtx.rect(j * GRID_WIDTH, i * GRID_WIDTH, GRID_WIDTH, GRID_WIDTH);
        }
    }
    thisCtx.stroke();
}
function refreshMotes() {
    let motes = canvasModule.motes;
    for (let i = 0; i < motes.length; ++i) {
        drawMote(motes[i].entity, motes[i].x, motes[i].y);
        if (motes[i].attackerId != 0) {
            drawMote(workspace.imgs.target, motes[i].x - MOTE_WIDTH / 2, motes[i].y - MOTE_WIDTH / 2)
        }
        if (workspace.isRunning && motes[i].isEndDevice) {
            drawStatus(motes[i].x + MOTE_WIDTH / 2, motes[i].y - MOTE_WIDTH / 2 - STATUS_WIDTH, motes[i].status);
        }
    }
}
function refreshLines() {
    let lines = canvasModule.lines;
    for (let i = 0; i < lines.length; ++i) {
        let line = lines[i];
        drawLine(line.ax, line.ay, line.bx, line.by)
    }
}

function drawMote(entity, x, y) {
    ctx.drawImage(entity, x - MOTE_WIDTH / 2, y - MOTE_WIDTH / 2)
}
function drawLine(ax, ay, bx, by) {
    ctx.beginPath();
    ctx.moveTo(ax, ay);
    ctx.lineTo(bx, by);
    ctx.stroke();
}
function drawStatus(x, y, status) {
    ctx.beginPath();
    if (status == STATUS_NORMAL) {
        ctx.fillStyle = '#0ed145';
        // 绘制圆的路径**
        ctx.arc(x, y, getStatusRadius(), 0, Math.PI * 2, false);
    } else {
        ctx.fillStyle = 'red'
        // 绘制圆的路径**
        ctx.arc(x, y, STATUS_WIDTH, 0, Math.PI * 2, false);
    }
    ctx.fill();
}
function getStatusRadius() {
    return STATUS_WIDTH * ((animeCounter + 1) / (ANIME_BOARDER + 1))
}

function getCLickObject(e) {
    x = e.layerX;
    y = e.layerY;
    for (let i = 0; i < canvasModule.motes.length; i++) {
        const element = canvasModule.motes[i];
        if (checkCollision(x, y, element.x, element.y)) {
            return element;
        }
    }
    return null;
}

function checkCollision(ax, ay, bx, by) {
    if (Math.abs(ax - bx) <= MOTE_WIDTH / 2 && Math.abs(ay - by) <= MOTE_WIDTH / 2) {
        return true
    }
    return false
}